package com.lee.mysql.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lee.mysql.dao.MenuDao;
import com.lee.mysql.dao.RoleDao;
import com.lee.mysql.dao.RoleMenuDao;
import com.lee.mysql.dao.UserRoleDao;
import com.lee.mysql.pojo.Menu;
import com.lee.mysql.pojo.Role;
import com.lee.mysql.pojo.RoleMenu;
import com.lee.mysql.pojo.User;
import com.lee.mysql.util.IdGenUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * 角色
 */
@Slf4j
@Service
@Transactional
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
public class RoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private RoleMenuDao roleMenuDao;
    @Autowired
    private UserRoleDao userRoleDao;



    /** 新增角色 */
    public int addRecord(Role record){
        record.setId(IdGenUtil.flakeId());
        record.setSysRole("0");
        return roleDao.insertOne(record);
    }

    /** 根据ID修改记录 */
    public int updateOneById(Role record){
        return roleDao.updateById(record);
    }

    /** 修改角色关联菜单 */
    public int updateRoleMenus(String roleId,List<String> menuIds){
        roleMenuDao.deleteByRoleId(roleId);

        if (menuIds.size() > 0){
            List<RoleMenu> list = new ArrayList<>();
            menuIds.forEach(menuId->{
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setMenuId(menuId);
                roleMenu.setRoleId(roleId);
                list.add(roleMenu);
            });

            return roleMenuDao.batchInsert(list);
        }else {
            return 1;
        }
    }

    /** 检测是否是系统角色 */
    public int checkSysRoles(List<String> idList){
        return roleDao.checkSysRoles(idList);
    }

    /** 根据ID批量删除记录 */
    public int deleteByIds(List<String> idList){
        roleMenuDao.deleteByRoleIds(idList);
        userRoleDao.deleteByRoleIds(idList);
        return roleDao.deleteByIds(idList);
    }

    /** 根据ID查询记录 */
    public Role queryOneById(String id){
        return roleDao.queryOneById(id);
    }


    /** 查询分页数据 */
    public PageInfo<Role> queryPaging(Role record, Integer page, Integer size){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if (user.getIsAdmin() != User.ISADMIN){
            record.setSysRole("0");
        }

        PageHelper.startPage(page, size);
        List<Role> list = roleDao.getList(record);

        return new PageInfo<>(list);
    }


    /** 查询所有角色 */
    public List<Role> queryAllRoles(){
        String notAdmin = null;
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if (user.getIsAdmin() != User.ISADMIN){
            notAdmin = "1";
        }
        return roleDao.queryAllRoles(notAdmin);
    }


    /** 获取角色拥有菜单 */
    public List<Menu> getRoleMenuList(String roleId){

        return menuDao.getRoleMenuList(roleId);
    }


}
